from django.db import models
from common.base_model import BaseModel
from api.shop.models import Shop


class GoodsCategory(models.Model):
    """
    商品分类信息
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256, verbose_name='商品分类名称', default='')
    parent = models.ForeignKey(to='self', null=True, blank=True, verbose_name='父类', on_delete=models.DO_NOTHING, related_name='sub_cat')
    logo = models.ImageField(verbose_name='分类logo图片', upload_to='uploads/goods_image')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = '商品分类'


class Goods(BaseModel):
    """
    商品信息
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256, verbose_name='商品名称', default='')
    picture = models.ImageField(verbose_name='商品图片', upload_to='uploads/goods_image')
    price = models.DecimalField(verbose_name='价格', decimal_places=2, max_digits=8, default=0)
    category = models.ForeignKey(GoodsCategory, null=True, blank=True, on_delete=models.DO_NOTHING, verbose_name='商品分类', related_name='goods')
    shop = models.ForeignKey(Shop, on_delete=models.DO_NOTHING, verbose_name='商铺拥有的商品', null=True, blank=True, related_name='goods')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = '商品'
        db_table = 'tb_good'


